class MainController < ApplicationController
	#protect_from_forgery :except => {:ohayo :post}
	skip_before_action :verify_authenticity_token
	def ohayo
		@user = User.find_by(user_id: sign_params[:user_id])
		if @user
			if @user.email != sign_params[:email]
				return render :json => {status: :error, descript: :information_unmatch, error_code: 233}.to_json
				#return
			end
			@user.last_login = DateTime.now
			@user.save
		else

			@user = User.new
			@user.email = sign_params[:email]
			@user.name = sign_params[:name]
			@user.user_id = sign_params[:user_id]
			@user.auth_token = sign_params[:auth_token]
			@user.save
		end
	end

	def post
		@user = User.find_by(user_id: post_params[:user_id])
		if @user.auth_token != post_params[:auth_token]
			return render :json => {status: :error, descript: :authentication_failed}.to_json
		end
		@record = Record.new
		@record.user_id = post_params[:user_id]
		@record.value = post_params[:value]
		@record.type = post_params[:type]
		#@record.time = DateTime.now
		@record.save
	end

	private

	def sign_params
		params.require(:sign).permit(:email, :user_id, :name, :auth_token)
	end

	def post_params
		params.require(:post).permit(:user_id, :value, :type, :auth_token)
	end
end
